package wec_wallet.ebo.com.activity;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import org.json.JSONObject;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import bassproject.ebo.com.ebobass.download.DownloadListActivity;
import bassproject.ebo.com.ebobass.http_utils.interfaces.IHttpPostListener;
import bassproject.ebo.com.ebobass.http_utils.utils.Ipost;
import bassproject.ebo.com.ebobass.qrcode.dommy.qrcode.QrcodeActivity;
import bassproject.ebo.com.ebobass.upload.UploadListActivity;
import bassproject.ebo.com.ebobass.utils.IToast;
import bassproject.ebo.com.ebobass.utils.Lg;
import bassproject.ebo.com.ebobass.utils.MyAppInfo;
import bassproject.ebo.com.ebobass.utils.MyUpdata;
import bassproject.ebo.com.ebobass.utils.OnDoubleClickListener;
import bassproject.ebo.com.ebobass.view.ProgressButton;
import wec_wallet.ebo.com.R;
import wec_wallet.ebo.com.utils.Functions;
import wec_wallet.ebo.com.utils.URLs;


public class TestActivity extends AbsImageActivity {


    private static final int PERMISSION_REQ_ID_WRITE_EXTERNAL_STORAGE = 3;
    ProgressButton btn;
    Button btn_Zxing;
    Button btn_head;
    Button btn_oss_upload;
    Button btn_download;
    Button btn_test_http;
    TextView tv_double;
    Button btn_check_updata;
    String apkname, info, url;
    int state;
    private int count_progress = 0;
    Handler mhandle = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    mhandle.sendMessageDelayed(mhandle.obtainMessage(1), 100);
                    count_progress += 5;
                    count_progress %= 100;
                    btn.setProgress(count_progress);
                    btn.setText("我是进度条按钮！");
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initViews();
        checkSelfPermissions();
    }


    @Override
    public void initViews() {
        btn = (ProgressButton) findViewById(R.id.button);
        btn.setOnClickListener(this);
        btn_Zxing = (Button) findViewById(R.id.zxing);
        btn_Zxing.setOnClickListener(this);
        btn_head = (Button) findViewById(R.id.btn_head);
        btn_head.setOnClickListener(this);
        btn_oss_upload = (Button) findViewById(R.id.btn_oss_upload);
        btn_oss_upload.setOnClickListener(this);
        btn_download = (Button) findViewById(R.id.btn_download);
        btn_download.setOnClickListener(this);
        mhandle.sendMessageDelayed(mhandle.obtainMessage(1), 1000);
        btn_test_http = (Button) findViewById(R.id.btn_test_http);
        btn_test_http.setOnClickListener(this);
        tv_double = (TextView) findViewById(R.id.tv_double);
        tv_double.setOnTouchListener(new OnDoubleClickListener(new OnDoubleClickListener.DoubleClickCallback() {
            @Override
            public void onDoubleClick() {
                show_Toast("双击");
            }
        }));
        btn_check_updata = (Button) findViewById(R.id.btn_check_updata);
        btn_check_updata.setOnClickListener(this);

    }

    private boolean checkSelfPermissions() {
        return
                checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, PERMISSION_REQ_ID_WRITE_EXTERNAL_STORAGE);
    }

    public boolean checkSelfPermission(String permission, int requestCode) {
        Lg.d("checkSelfPermission " + permission + " " + requestCode);
        if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
//            Lg.d("ffffffff");
            ActivityCompat.requestPermissions(this, new String[]{permission}, requestCode);
            return false;
        }
//        Lg.d("ttttttt");
//        if (Manifest.permission.CAMERA.equals(permission)) {
//            ((AGApplication) getApplication()).initWorkerThread();
//        }
        return true;
    }

    @Override
    public void onRequestPermissionsResult(int requestCode,
                                           @NonNull String permissions[], @NonNull int[] grantResults) {
        Lg.d("onRequestPermissionsResult " + requestCode + " " + Arrays.toString(permissions) + " " + Arrays.toString(grantResults));
        switch (requestCode) {
//            case ConstantApp.PERMISSION_REQ_ID_RECORD_AUDIO: {
//                if (grantResults.length > 0
//                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//                    checkSelfPermission(Manifest.permission.CAMERA, ConstantApp.PERMISSION_REQ_ID_CAMERA);
//                } else {
//                    IToast.show("请手动打开麦克风权限");
//                }
//                break;
//            }
//            case ConstantApp.PERMISSION_REQ_ID_CAMERA: {
//                if (grantResults.length > 0
//                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//                    checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, ConstantApp.PERMISSION_REQ_ID_WRITE_EXTERNAL_STORAGE);
//                    ((AGApplication) getApplication()).initWorkerThread();
//                } else {
//                    IToast.show("请手动打开相机权限");
//                }
//                break;
//            }
            case PERMISSION_REQ_ID_WRITE_EXTERNAL_STORAGE: {
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                } else {
                    IToast.show("请手动打开访问SD卡权限");
                }
                break;
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button:
                Intent intent0 = new Intent();
                intent0.setClass(this, RefreshActivity.class);
                startActivity(intent0);
                break;
            case R.id.btn_download:
                Intent intent = new Intent();
                intent.setClass(this, DownloadListActivity.class);
                intent.putExtra("url", "http://gdown.baidu.com/data/wisegame/2d5bf81de4e0ca42/weixin_1041.apk");
                intent.putExtra("name", "weixin_1041.apk");
                startActivity(intent);
                break;
            case R.id.zxing:
                Intent intent1 = new Intent();
                intent1.setClass(this, QrcodeActivity.class);
                startActivity(intent1);
                break;
            case R.id.btn_head:
                mImagePicker.showMenu(btn_head, this);
                break;
            case R.id.btn_oss_upload:
                startActivity(new Intent().setClass(this, UploadListActivity.class));
                break;
            case R.id.btn_test_http:
                IHttpPostListener postListener = new IHttpPostListener() {
                    @Override
                    public void onTimeOut() {

                    }
                    @Override
                    public void responseSuccess(String result) {
                        if (Ipost.isTimeOut(result)) {
                            return;
                        }
                        show_Toast("返回结果：" + result);
                    }
                };
                Functions.post(this, URLs.PAYCOIN, null, postListener, true, true);
                break;
            case R.id.btn_check_updata:
                //检查更新
                if (!MyUpdata.finishCheckUpdata) {
                    Map<String, Object> postParams = new HashMap<String, Object>();
                    postParams.put("id", MyAppInfo.getVersionCode());
                    Lg.d("MyAppInfo.getVersionCode() = " + MyAppInfo.getVersionCode());
                    Functions.post(this, URLs.CHECK_VERSION_UPDATA, postParams, new PostVersionCheckUpdata(), true, false);
                }
                break;

        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exit();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }


    class PostVersionCheckUpdata implements IHttpPostListener {
        @Override
        public void onTimeOut() {

        }
        @Override
        public void responseSuccess(String result) {
            if (Ipost.isTimeOut(result)) {
                return;
            }
            Lg.d("result= " + result);
            if (null != result) {
                try {
                    JSONObject resultObject = new JSONObject(result);
                    if (resultObject.getInt("has_update") == 1) {
                        //有更新
                        if (resultObject.has("data")) {
                            JSONObject jb = new JSONObject(resultObject.getString("data"));
                            String code = jb.getString("code");
                            apkname = jb.getString("name") + code + ".apk";
                            info = jb.getString("info");
                            state = jb.getInt("state");
                        }
                        if (resultObject.has("url")) {
                            url = resultObject.getString("url");
                        }
                        MyUpdata.checkUpdata(TestActivity.this, state, apkname, url, info);
                    } else {
                        //无更新
                        MyUpdata.finishCheckUpdata = true;
                    }
                } catch (Exception e) {
                    // TODO: handle exception
                }

            }
        }
    }
}
